package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.neng.tools.AppException;
import com.vma.offer.service.OfferService;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenzui on 2017/4/7.
 */
public class OfferController extends Controller {
    public void add(){
        try{
            OfferService.addOffer(getParaMap());
            renderJson(ReturnsUtil.returnSuccess("添加商品成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateOffer(){
        try{
            OfferService.updateOffer(getParaMap());
            renderJson(ReturnsUtil.returnSuccess("更新商品成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryOfferList(){
        try {
            Integer cPage = getParaToInt("cPage");
            Integer pSize = getParaToInt("pSize");
            String offerName = getPara("marketName");
            Integer typeId = getParaToInt("type");
            Integer brandId = getParaToInt("brandId");
            List param = new ArrayList();
            String condition = "";
            if(offerName != null && !"".equals(offerName)){
                param.add("%"+offerName+"%");
                condition +=" and o.offer_name like ? ";
            }
            if(typeId != null && typeId != 0){
                param.add(typeId);
                condition += " and o.offer_type = ?";
            }
            if(brandId != null && brandId != 0){
                param.add(brandId);
                condition += " and o.brand_id = ?";
            }


            Page<Record> offerPage = null;
//            if(offerName == null || "".equals(offerName)){
//                offerPage= Db.paginate(cPage,pSize,"SELECT DISTINCT o.id,o.offer_name,o.in_price,o.out_price,o.o_status,o.create_time,ot.offer_type_name,b.brand_name ",
//                        " FROM offer o,offer_type ot,brand b " +
//                                "WHERE o.if_delete = 0" +
//                                " and b.id = o.brand_id and o.offer_type = ot.id");
//            }else {
            offerPage= Db.paginate(cPage,pSize,"SELECT DISTINCT o.id,o.offer_name,o.in_price,o.out_price,o.o_status,o.create_time,ot.offer_type_name,b.brand_name ",
                    " FROM offer o,offer_type ot,brand b " +
                            "WHERE o.if_delete = 0" +
                            " and b.id = o.brand_id and o.offer_type = ot.id " +
                      condition,param.toArray());
//            }
            renderJson(ReturnsUtil.returnSuccess(offerPage));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * 更新
     */
    public void updateStatus(){
        try {
            String id = getPara("id");
            Offer offer = Offer.dao.findById(id);
            offer.setOStatus(offer.getOStatus().equals("1")?"0":"1").update();
            renderJson(ReturnsUtil.returnSuccess("更新成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }


    public void deleteOffer(){
        try {
            String id = getPara("id");
            //判断是否有在未付款订单中存在

            Record record = Db.findFirst("SELECT count(1) order_num FROM order_offer oo ,orderList ol\n" +
                    "WHERE ol.id = oo.order_id AND oo.offer_id =? AND ol.status = 1",id);
            Integer orderNum = Integer.valueOf(record.get("order_num").toString());

            if(orderNum > 0){
                throw new AppException("-1","该商品存在未付款订单");
            }

            Offer offer = Offer.dao.findById(id);
            offer.setIfDelete("1").update();


            renderJson(ReturnsUtil.returnSuccess("删除成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
    public void addOfferInit(){

        Map initData = new HashMap();

        List<OfferType> types = OfferType.dao.find("SELECT offer_type_name,id FROM offer_type  WHERE if_delete = 0 ORDER BY id");

        List<Brand> brands = Brand.dao.find("SELECT id,brand_name FROM brand WHERE if_delete = 0");

        List<SpecItem> colors = SpecItem.dao.find("SELECT id,spec_value FROM spec_item WHERE spec_group_id = 1");

        List<SpecItem> lengths = SpecItem.dao.find("SELECT id,spec_value FROM spec_item WHERE spec_group_id = 2");

        initData.put("types",types);

        initData.put("brands",brands);

        initData.put("colors",colors);

        initData.put("lengths",lengths);

        setAttr("initData",initData);

        renderVelocity("/shop/offer/offer/offerdetail.html");
    }

    public void getOfferDetail(){
        String id = getPara("id");
        Map result = OfferService.getOfferDetail(id);
        setAttr("initData",result);
        renderVelocity("/shop/offer/offer/offerdetail.html");
    }

    public void initList(){
        List<OfferType> types = OfferType.dao.find("SELECT offer_type_name,id FROM offer_type WHERE if_delete = 0 ORDER BY id");

        List<Brand> brands = Brand.dao.find("SELECT id,brand_name FROM brand WHERE if_delete = 0");

        setAttr("types",types);

        setAttr("brands",brands);

        renderVelocity("/shop/offer/offer/list.html");

    }

    public void deleImg(){
        try {
            String id = getPara("imgId");
            //判断是否有在未付款订单中存在
            OfferImg img = OfferImg.dao.findById(id);
            img.delete();
            renderJson(ReturnsUtil.returnSuccess("删除成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
